import 'dart:convert';
import 'package:encrypt/encrypt.dart';

class DecryptUtil {
  static const String _key = '12345678123456781234567812345678';

  /// 解密方法
  static String decrypt(String encryptedBase64, String ivStr) {
    try {
      // 将Base64字符串转换为字节数组
      final encryptedBytes = base64.decode(encryptedBase64);

      // 创建Key和IV
      final key = Key.fromUtf8(_key);
      final iv = IV.fromUtf8(ivStr);

      // 初始化AES解密器
      final encrypter = Encrypter(AES(
        key,
        mode: AESMode.cbc,
        padding: 'PKCS7',
      ));

      // 执行解密
      final decrypted = encrypter.decryptBytes(Encrypted(encryptedBytes), iv: iv);
      return utf8.decode(decrypted);
    } catch (e) {
      throw Exception('解密失败: $e');
    }
  }
}
